<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">跟进客户：</label>
                                    <div class="layui-input-inline">
                                        <select name="followCustomer" lay-search></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" layui-form-keyDownSearch="true" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="customerFollowRecordBtnAdd" class="layui-btn icon-btn color-reseda">添加
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="customerFollowRecordTable" lay-filter="customerFollowRecordTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="customerFollowRecordTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
    {{# if(d.status =="pendingReview"){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="process">审核</a>
    {{# } }}
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="customerFollowRecordForm">
    <form lay-filter="customerFollowRecordForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <input name="approverName" id="approverNameHidden" type="hidden"/>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">客户类型</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select name="customerType" lay-filter="customerType">
                        <option value="">请选择客户类型</option>
                        <option value="potential">潜在客户</option>
                        <option value="Intended">意向客户</option>
                        <option value="formal">正式客户</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">跟进类型</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select name="followType" lay-verify="required" required lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">跟进客户</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select name="followCustomer" lay-filter="followCustomer" lay-verify="required" required
                            lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">跟进对象</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select name="followTarget" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">跟进方式</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select name="followWay" lay-verify="required" required lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">跟进结果类型</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select name="followResult" lay-verify="required" required lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">产生费用</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input name="cost" placeholder="请输入产生费用" type="text" class="layui-input"
                           lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">跟进结果描述</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input name="followDescribe" placeholder="请输入跟进结果描述" type="text" class="layui-input"
                           lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">审批人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select name="approver" lay-filter="approver" lay-verify="required" required lay-search></select>
                </div>
            </div>
        </div>

        <div class="layui-col-xs12">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">备注</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <textarea name="remark" placeholder="请输入备注" maxlength="200" class="layui-textarea"></textarea>
                </div>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="customerFollowRecordFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 详情表单弹窗 -->
<style>
    .width105 {
        width: 105px;
    }

    .borderNone {
        border: none;
        color: red;
    }
</style>
<script type="text/html" id="customerFollowRecordDetailForm">
    <form lay-filter="customerFollowRecordDetailForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">客户类型</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <select name="customerType" class="selectDisabled">
                        <option value="">请选择客户类型</option>
                        <option value="potential">潜在客户</option>
                        <option value="Intended">意向客户</option>
                        <option value="formal">正式客户</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">跟进类型</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <select name="followType" class="selectDisabled" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">跟进客户</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <select name="followCustomer" class="selectDisabled" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">跟进对象</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <select name="followTarget" class="selectDisabled" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">跟进对象职务</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <input name="jobDesc" type="text" class="layui-input selectDisabled"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">跟进对象电话</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <input name="telephone" type="text" class="layui-input selectDisabled"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">跟进方式</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <select name="followWay" class="selectDisabled" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">跟进结果类型</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <select name="followResult" class="selectDisabled" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">产生费用</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <input name="cost" type="text" class="layui-input selectDisabled"/>
                </div>
                <!--<div class="layui-form-mid layui-word-aux">单位：元</div>-->
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">跟进结果描述</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <input name="followDescribe" type="text" class="layui-input selectDisabled"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">提交时间</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <input name="createTime" type="text" class="layui-input selectDisabled"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">审批时间</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <input name="checkTime" type="text" class="layui-input selectDisabled"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label width105">审批人</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <select name="approver" class="selectDisabled" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs12">
            <div class="layui-form-item">
                <label class="layui-form-label width105">备注</label>
                <div class="layui-input-block" style="margin-left: 135px;">
                    <textarea name="remark" id="followRemark" maxlength="200" class="layui-textarea"></textarea>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" id="customerFollowRecordDetailFormSubmit"
                    lay-filter="customerFollowRecordDetailFormSubmit" lay-submit>保存
            </button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#customerFollowRecordTable',
            url: config.base + 'enterprise/sell/admin/customerFollowRecord/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                , {field: 'customerTypeDesc', align: 'center', title: '客户类型'}
                , {field: 'followTypeName', align: 'center', title: '跟进类型'}
                , {field: 'followCustomerName', align: 'center', title: '跟进客户', width: 160}
                , {field: 'followTargetName', align: 'center', title: '跟进对象'}
                , {field: 'followWayName', align: 'center', title: '跟进方式'}
                , {field: 'followResultName', align: 'center', title: '跟进结果', width: 180}
                , {field: 'cost', align: 'center', title: '产生费用'}
                , {field: 'followDescribe', align: 'center', title: '跟进结果描述', width: 160}
                , {field: 'approverName', align: 'center', title: '审批人'}
                , {field: 'statusDesc', align: 'center', title: '状态'}
                , {field: 'createTime', align: 'center', title: '创建时间', width: 160}
                , {align: 'center', toolbar: '#customerFollowRecordTableBar', title: '操作', width: 150}
            ]],
            done: function (res, curr, count) {

            }
        });

        // 工具条点击事件
        table.on('tool(customerFollowRecordTable)', function (obj) {
            var data = obj.data;
            var type = 0;//0-详情，1-审核
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            } else if (obj.event === 'detail') {//详情
                showDetail(data, type);
            } else if (obj.event === 'process') {//审核
                type = 1;
                showDetail(data, type);
            }
        });

        //监听排序
        table.on('sort(customerFollowRecordTable)', function (obj) {
            table.reload('customerFollowRecordTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#customerFollowRecordBtnAdd').click(function () {
            showEditModel();
        });

        // 搜索
        //获取客户数据
        admin.req('enterprise/sell/admin/customersInfo/getCustomerByType', {}, function (res) {
            if (res.code == 200) {
                loadSelectHtml(res.data, "请选择跟进客户", "followCustomer", "customerName");
            } else {
                layer.msg('获取客户失败', {icon: 2, time: 1000});
            }
        }, 'GET');

        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        //详情弹窗
        function showDetail(data, type) {
            admin.open({
                type: 1,
                area: '760px',
                offset: '120px',
                title: type === 1 ? '审核跟进记录' : '跟进记录详情',
                content: $('#customerFollowRecordDetailForm').html(),
                success: function () {
                    if (data) {
                        var option = $("form select[name='customerType']").find("option");
                        layui.each(option, function (index, item) {
                            if (data.customerType == item.text) {
                                data.customerType = item.value;
                            }
                        });
                    }

                    form.val('customerFollowRecordDetailForm', data);

                    getCompany(data);// 获取客户数据

                    getContactInfo(data);// 获取客户联系人

                    getFollowType(data);// 获取跟进类型

                    getFollowWay(data);// 获取跟进方式

                    getFollowResultType(data);// 获取跟进结果类型

                    getUsers(data);// 获取审批人

                    $(".selectDisabled").attr("disabled", "disabled");
                    $(".selectDisabled").attr("style", "border: none;");

                    if (type == 0) {
                        $('#followRemark').attr("disabled", true);
                        $('#customerFollowRecordDetailFormSubmit').hide();//提交按钮隐藏
                    } else if (type == 1) {
                        $('#followRemark').attr("disabled", false);
                        $('#customerFollowRecordDetailFormSubmit').show();//提交按钮显示

                        form.on('submit(customerFollowRecordDetailFormSubmit)', function (d) {
                            layer.load(2);
                            admin.req('enterprise/sell/admin/customerFollowRecord/audit', d.field, function (res) {
                                if (res.code == 200) {
                                    layer.closeAll('loading');
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('customerFollowRecordTable');
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'PUT')
                        });
                    }

                }
            });
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '760px',
                offset: '120px',
                title: data ? '修改跟进记录管理' : '添加跟进记录管理',
                content: $('#customerFollowRecordForm').html(),
                success: function () {
                    if (data) {
                        var option = $("form select[name='customerType']").find("option");
                        layui.each(option, function (index, item) {
                            if (data.customerType == item.text) {
                                data.customerType = item.value;
                            }
                        });
                    }

                    form.val('customerFollowRecordForm', data);


                    form.on('select(customerType)', function (data) {
                        getCompany(data);// 获取客户数据
                    });
                    form.on('select(followCustomer)', function (data) {
                        getContactInfo(data);// 获取客户联系人
                    })

                    getFollowType(data);// 获取跟进类型

                    getFollowWay(data);// 获取跟进方式

                    getFollowResultType(data);// 获取跟进结果类型

                    getUsers(data);// 获取审批人
                    form.on('select(approver)', function (data) {//审批人姓名
                        admin.req('user/admin/user/getUserInfoByUserId', {userId: data.value}, (res) => {
                            if (res.code == '200') {
                                $("#approverNameHidden").val(res.data.nickname);
                            }
                        }, 'GET');
                    });

                    // 表单提交事件
                    form.on('submit(customerFollowRecordFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/sell/admin/customerFollowRecord/update' : 'enterprise/sell/admin/customerFollowRecord/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('customerFollowRecordTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 获取客户数据
        function getCompany(data) {
            admin.req('enterprise/sell/admin/customersInfo/getCustomerByType', {customerTypeName: data.value}, function (res) {
                if (res.code == 200) {
                    loadSelectHtml(res.data, "请选择客户类型", "followCustomer", "customerName");

                    if (data && data.followCustomer) {
                        form.val('customerFollowRecordForm', data);
                        form.val('customerFollowRecordDetailForm', data);
                    }
                } else {
                    loadSelectHtml(res.data, "请选择客户类型", "followCustomer", "customerName");
                    layer.msg(res.msg, {icon: 2, time: 1000});
                }
            }, 'GET');
        }

        // 获取客户联系人
        function getContactInfo(data) {
            admin.req('enterprise/sell/admin/contactInfo/list', {selCustomerId: data.value}, function (res) {
                if (res.code == 200) {
                    loadSelectHtml(res.data, "请选择跟进对象", "followTarget", "name")

                    if (data && data.followTarget) {
                        form.val('customerFollowRecordForm', data);
                        form.val('customerFollowRecordDetailForm', data);
                    }
                } else {
                    layer.msg('获取联系人失败', {icon: 2, time: 1000});
                }
            }, 'GET');
        }

        // 获取跟进类型
        function getFollowType(data) {
            admin.req('enterprise/sell/admin/customerFollowType/list', {limit: 9999}, function (res) {
                if (res.code == 200) {
                    var html = '<option value="">请选择跟进类型</option>';
                    for (var k = 0; k < res.data.length; k++) {
                        html += '<option value="' + res.data[k].code + '">' + res.data[k].name + '</option>';
                    }
                    $("form select[name='followType']").html(html);
                    form.render('select');
                    if (data && data.followType) {
                        form.val('customerFollowRecordForm', data);
                        form.val('customerFollowRecordDetailForm', data);
                    }

                } else {
                    layer.msg('获取跟进类型失败', {icon: 2, time: 1000});
                }
            }, 'GET');
        }

        // 获取跟进方式
        function getFollowWay(data) {
            admin.req('enterprise/sell/admin/customerFollowWay/list', {limit: 9999}, function (res) {
                if (res.code == 200) {
                    var html = '<option value="">请选择跟进方式</option>';
                    for (var k = 0; k < res.data.length; k++) {
                        html += '<option value="' + res.data[k].code + '">' + res.data[k].name + '</option>';
                    }
                    $("form select[name='followWay']").html(html);
                    form.render('select');
                    if (data && data.followWay) {
                        form.val('customerFollowRecordForm', data);
                        form.val('customerFollowRecordDetailForm', data);
                    }

                } else {
                    layer.msg('获取跟进方式失败', {icon: 2, time: 1000});
                }
            }, 'GET');
        }

        //获取跟进结果类型
        function getFollowResultType(data) {
            admin.req('enterprise/sell/admin/customerFollowResultType/list', {limit: 9999}, function (res) {
                if (res.code == 200) {
                    var html = '<option value="">请选择跟进结果类型</option>';
                    for (var k = 0; k < res.data.length; k++) {
                        html += '<option value="' + res.data[k].code + '">' + res.data[k].name + '</option>';
                    }
                    $("form select[name='followResult']").html(html);
                    form.render('select');
                    if (data && data.followResult) {
                        form.val('customerFollowRecordForm', data);
                        form.val('customerFollowRecordDetailForm', data);
                    }

                } else {
                    layer.msg('获取跟进结果类型失败', {icon: 2, time: 1000});
                }
            }, 'GET');
        }

        //获取审批人
        function getUsers(data) {
            admin.req('user/admin/user/list', {limit: 9999}, function (res) {
                if (res.code == 200) {
                    var html = '<option value="">请选择审批人</option>';
                    for (var k = 0; k < res.data.length; k++) {
                        html += '<option value="' + res.data[k].id + '">' + res.data[k].username + '</option>';
                    }
                    $("form select[name='approver']").html(html);
                    form.render('select');
                    if (data && data.approver) {
                        form.val('customerFollowRecordForm', data);
                        form.val('customerFollowRecordDetailForm', data);
                    }

                } else {
                    layer.msg('获取审批人失败', {icon: 2, time: 1000});
                }
            }, 'GET');
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/sell/admin/customerFollowRecord/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }


        /**
         * 加载地址HTML
         * @param data 下拉菜单数据
         * @param tips select 默认提示
         * @param htmltarget html要插入的位置
         * @param name  下拉框需要显示值的属性
         */
        function loadSelectHtml(data, tips, htmltarget, name) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                html += `<option value='${data[i].id}'>${data[i][name]}</option>`;
            }
            $("[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>